const express = require("express");
const router = express.Router();
const { seniverse } = require("../../utils/zx.js");
const { setIpJsonDb } = require("../../utils/handIpJson.js");
const { isPublicIp } = require("../../utils/ip.js");

// 获取天气  心知天气
router.get("/now", async function (req, res) {
  const ipxx = req.ip || req.socket.remoteAddress;
  const ip = isPublicIp(ipxx) ? ipxx : "ip";
  const { location } = req.query;
  const pip = location || ip;
  // 存入ip
  // await setIpJsonDb(pip, "/now => /weather/now.json");
  seniverse
    .get("/weather/now.json", {
      location: pip
    })
    .then((resp) => {
      res.status(200).send({
        code: 1,
        data: resp,
        message: "success"
      });
    })
    .catch((err) => {
      res.status(200).send({
        code: 1,
        data: {
          location: {
            id: "WWYMRT0VRMUG",
            name: location,
            country: "CN",
            path: "xx,xx,xx,中国"
          },
          now: {
            text: "多云",
            code: "4",
            temperature: "29",
            feels_like: "34",
            pressure: "997",
            humidity: "79",
            visibility: "9.9",
            wind_direction: "东南",
            wind_direction_degree: "140",
            wind_speed: "10.0",
            wind_scale: "2",
            clouds: "50",
            dew_point: ""
          },
          last_update: "2025-08-19T15:15:20+08:00"
        },
        requestUlr: err.request?.res?.responseUrl,
        error: String(err),
        errpath: err.request.path,
        errData: err.response.data,
        message: "success"
      });
      // res.status(200).send({
      //   code: 0,
      //   data: String(err),
      //   message: "error"
      // });
    });
});

router.get("/daily", async function (req, res) {
  const { location } = req.query;
  seniverse
    .get("/weather/daily.json", { location })
    .then((resp) => {
      res.status(200).send({
        code: 1,
        data: resp,
        message: "success"
      });
    })
    .catch((err) => {
      res.status(200).send({
        code: 0,
        data: err,
        message: "error"
      });
    });
});

// 空气质量 air
router.get("/air", async function (req, res) {
  const { location } = req.query;
  seniverse
    .get("/air/now.json", { location })
    .then((resp) => {
      res.status(200).send({
        code: 1,
        data: resp,
        message: "success"
      });
    })
    .catch((err) => {
      res.status(200).send({
        code: 0,
        data: err,
        message: "error"
      });
    });
});

// 24小时天气
router.get("/hourly", async function (req, res) {
  const { location } = req.query;
  seniverse
    .get("/weather/hourly.json", { location })
    .then((resp) => {
      res.status(200).send({
        code: 1,
        data: resp,
        message: "success"
      });
    })
    .catch((err) => {
      res.status(200).send({
        code: 0,
        data: err,
        message: "error"
      });
    });
});

//  日出日落
router.get("/sun", async function (req, res) {
  const { location } = req.query;
  seniverse
    .get("/geo/sun.json", { location })
    .then((resp) => {
      res.status(200).send({
        code: 1,
        data: resp,
        message: "success"
      });
    })
    .catch((err) => {
      res.status(200).send({
        code: 0,
        data: err,
        message: "error"
      });
    });
});

// 生活指数 suggestion
router.get("/suggestion", async function (req, res) {
  const { location } = req.query;
  seniverse
    .get("/life/suggestion.json", { location })
    .then((resp) => {
      res.status(200).send({
        code: 1,
        data: resp,
        message: "success"
      });
    })
    .catch((err) => {
      res.status(200).send({
        code: 0,
        data: err,
        message: "error"
      });
    });
});

module.exports = router;
